﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLPM
{
    public partial class frmDonGiaThuoc : DevComponents.DotNetBar.Office2007Form
    {
        public frmDonGiaThuoc()
        {
            InitializeComponent();
        }


        public void LoadDonGiaTheoMaThuoc()
        {
            DonGiaBUS dgBus = new DonGiaBUS();
            DataTable dt = new DataTable();
            dt = dgBus.LayBangDonGiaTheoMaThuoc(cmbTenLoaiThuoc.SelectedValue.ToString());
            dt.Columns.Add("STT");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int j = i + 1;
                dt.Rows[i]["STT"] = j.ToString();
            }
            dtgvChiTietDonGia_Selected.DataSource = dt;
            dtgvChiTietDonGia_Selected.Columns["STT"].Width = 40;
            dtgvChiTietDonGia_Selected.Columns["STT"].DisplayIndex = 0;
            dtgvChiTietDonGia_Selected.Columns["STT"].ReadOnly = true;
        }

        public void LoadDonGia()
        {
            DonGiaBUS dgBus = new DonGiaBUS();
            DataTable dt = new DataTable();
            dt = dgBus.LayBangDonGia();
            dt.Columns.Add("STT");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int j = i + 1;
                dt.Rows[i]["STT"] = j.ToString();
            }
            dtgvChiTietDonGia_All.DataSource = dt;
            dtgvChiTietDonGia_All.Columns["STT"].Width = 40;
            dtgvChiTietDonGia_All.Columns["STT"].DisplayIndex = 0;
            dtgvChiTietDonGia_All.Columns["STT"].ReadOnly = true;
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {         
            Close();
        }


        public void Load_TenDonVi()
        {
            DonViBUS dvBus = new DonViBUS();
            DataTable dt = new DataTable();
            dt = dvBus.LayBangDonVi();
            cmbTenDonVi.DataSource = dt;
            cmbTenDonVi.DisplayMember = "TenDV";
            cmbTenDonVi.ValueMember = "MaDV";
        }
        public void Load_TenLoaiThuoc()
        {
            ThuocBUS thuocBus = new ThuocBUS();
            DataTable dt = new DataTable();
            dt = thuocBus.LayBangThuoc();
            cmbTenLoaiThuoc.DataSource = dt;
            cmbTenLoaiThuoc.DisplayMember = "TenLoaiThuoc";
            cmbTenLoaiThuoc.ValueMember = "MaLoaiThuoc";
        }

        private void frmDonGiaThuoc_Load(object sender, EventArgs e)
        {
            Load_TenDonVi();
            Load_TenLoaiThuoc();
            LoadDonGiaTheoMaThuoc();
        }

        private void btnThemDonGia_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thêm đơn giá??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                try
                {
                    //lay thong tin Dongia
                    DonGiaDTO dongiaDto = new DonGiaDTO();
                    DonGiaBUS dongiaBus = new DonGiaBUS();

                    dongiaDto.MaLoaiThuoc = this.cmbTenLoaiThuoc.SelectedValue.ToString();
                    dongiaDto.MaDV = this.cmbTenDonVi.SelectedValue.ToString();
                    dongiaDto.DonGia = Convert.ToInt32(this.txtDonGia.Text);

                    dongiaBus.ThemDonGia(dongiaDto);
                    LoadDonGiaTheoMaThuoc();
                    LoadDonGia();
                }
                catch
                {
                    MessageBox.Show("Insert Error! Check your information again!!!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }
        }

        private void cmbTenLoaiThuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            grbChiTietDonGia_Selected.Visible = true;
            LoadDonGiaTheoMaThuoc();
        }

        private void btnXemTatCa_Click(object sender, EventArgs e)
        {
            grbChiTietDonGia_All.Visible = true;
            grbChiTietDonGia_Selected.Visible = false;
            LoadDonGia();
        }

        private void dtgvChiTietDonGia_Selected_Click(object sender, EventArgs e)
        {
            DataGridViewRow dtgvR = dtgvChiTietDonGia_Selected.CurrentRow;
            cmbTenLoaiThuoc.Text = dtgvR.Cells["TenLoaiThuoc"].Value.ToString();
            cmbTenDonVi.Text = dtgvR.Cells["TenDV"].Value.ToString();
            txtDonGia.Text = dtgvR.Cells["DonGia"].Value.ToString();
        }

        private void dtgvChiTietDonGia_All_Click(object sender, EventArgs e)
        {
            DataGridViewRow dtgvR = dtgvChiTietDonGia_All.CurrentRow;
            cmbTenLoaiThuoc.Text = dtgvR.Cells["TenLoaiThuoc3"].Value.ToString();
            cmbTenDonVi.Text = dtgvR.Cells["TenDV3"].Value.ToString();
            txtDonGia.Text = dtgvR.Cells["DonGia3"].Value.ToString();
        }

        private void btnXoaDG_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa đơn giá??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                DonGiaBUS dgBus = new DonGiaBUS();
                dgBus.XoaDonGia(cmbTenLoaiThuoc.SelectedValue.ToString(), cmbTenDonVi.SelectedValue.ToString());
                LoadDonGia();
                LoadDonGiaTheoMaThuoc();
            }
        }

        private void btnSuaDonGia_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn sửa đơn giá??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                DonGiaDTO dgDto = new DonGiaDTO();
                DonGiaBUS dgBus = new DonGiaBUS();
                dgDto.MaLoaiThuoc = cmbTenLoaiThuoc.SelectedValue.ToString();
                dgDto.MaDV = cmbTenDonVi.SelectedValue.ToString();
                dgDto.DonGia = Convert.ToInt32(this.txtDonGia.Text);

                dgBus.SuaDonGia(dgDto);
                LoadDonGia();
                LoadDonGiaTheoMaThuoc();
            }
        }



    }
}